package com.seu.spring.simple.aop;

import java.lang.reflect.Method;

/**
 * 前置通知
 *
 * @author liangfeihu
 * @since 2018/12/27
 */
public class BeforeAdvice implements Advice {
    private Object bean;
    private MethodInvocation methodInvocation;

    public BeforeAdvice(Object bean, MethodInvocation methodInvocation) {
        this.bean = bean;
        this.methodInvocation = methodInvocation;
    }

    /**
     * Processes a method invocation on a proxy instance and returns
     * the result.  This method will be invoked on an invocation handler
     * when a method is invoked on a proxy instance that it is
     * associated with.
     * 处理代理实例上的方法调用并返回结果。当在与之关联的代理实例上调用方法时，将在调用处理程序上调用此方法。
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 在目标方法执行前调用通知
        methodInvocation.invoke();
        return method.invoke(bean, args);
    }

}
